package com.jsj.service;

import com.jsj.dao.LabelDao;
import com.jsj.dao.NumberDao;
import com.jsj.dao.ProblemDao;
import com.jsj.domain.Label;
import com.jsj.domain.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProblemService {
    private ProblemDao problemDao;
    private LabelDao labelDao;
    private NumberDao numberDao;
    @Autowired

    public void setProblemDao(ProblemDao problemDao) {
        this.problemDao = problemDao;
    }

    @Autowired
    public void setLabelDao(LabelDao labelDao) {
        this.labelDao = labelDao;
    }
    @Autowired
    public void setNumberDao(NumberDao numberDao) {
        this.numberDao = numberDao;
    }
    public List<Problem> queryByCreator(String creator) {
        List<Problem> problems = problemDao.queryByCreator(creator);
        if (problems != null) {
            for (Problem problem : problems) {
                problem.setLabel(labelDao.queryById(problem.getLabelId()));
            }
        }
        return problems;
    }

    public Problem queryById(int id) {
        Problem problem = problemDao.queryById(id);
        if (problem != null) {
            problem.setLabel(labelDao.queryById(problem.getLabelId()));
            problem.setNumber(numberDao.queryById(problem.getId()));
        }
        return problem;
    }

    public List<Problem> queryByTopic(int topicId) {
        List<Problem> problems = problemDao.queryByTopic(topicId);
        if (problems != null) {
            for (Problem problem : problems) {
                problem.setLabel(labelDao.queryById(problem.getLabelId()));
            }
        }
        return problems;
    }
    public List<Problem> queryByTopicId(int topicId) {
        List<Problem> problems = problemDao.queryByTopicId(topicId);
        if (problems != null) {
            for (Problem problem : problems) {
                System.out.println("problemid="+problem.getId());
//                problem.setLabel(labelDao.queryById(problem.getLabelId()));
                problem.setNumber(numberDao.queryById(problem.getId()));
            }
        }
        return problems;
    }

    public List<String> queryTitleByTopicId(int topicId){
        List<String> titiles=problemDao.queryTitleByTopicId(topicId);
        return titiles;
    }

    public Integer insert(Problem problem) {
        return problemDao.insert(problem);
    }

    public Integer delete(int id) {
        return problemDao.delete(id);
    }

    public Integer update(Problem problem) {
        return problemDao.update(problem);
    }
    public Problem queryNumber(Integer id) {
        return problemDao.queryNumber(id);
    }
}
